﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfGeebaServer.ContentManage;
using WcfGeebaServer.Users;
using WcfGeebaServer.DBDAL;

namespace WcfGeebaServer.Policies
{
    [Serializable]
    public class GPolicy
    {
        #region Fields

        //private List<String> _rules;

        #endregion

        #region Ctor

        public GPolicy()
        {

        }

        #endregion

        #region Methods

        //public void AddRule(String rule) {
        //    this._rules.Add(rule);
        //}

        public bool CanPublishMessage(Member member,GSubForum subForum,GThread message)
        {
            bool res = GForum.GetForum().IsMember(member.Username) &&
                member.IsActive && IsMessageCompatibleToSubForum(subForum,message);

            if (res == false)
            {
                MemberDA.getMemberDA().setLawBreaker(member.Username, true);
            }
            return res;
        }

        public bool CanEditThread(GThread thread, Member member)
        {
            bool res = thread.publishedBy.Equals(member);
            
            return res;
        }

        public bool CanDeleteThread(GThread thread, Member member, GSubForum subForum)
        {
            bool res = CanEditThread(thread, member) ||
                   subForum.ContainsModerator(member) ||
                   GForum.GetForum().Admin.Equals(member);

            return res;

        }

        public bool CanAddModerator(Member member, GSubForum subForum)
        {
            bool res = SubForumDA.GetSubForumDA().GetMessageCountByMember(member, subForum) >= 5;

            return res;
        }

        public bool CanRemoveModerator(Member member, GSubForum subForum)
        {
            bool res = true;
            res = res & subForum.ContainsModerator(member);
            res = res & (subForum.getModerators().Count > 1);
            GThread last = subForum.GetLastMessage(member);
            res = res & ((last == null) || (last.PublishDate < DateTime.Now.AddHours(-1)));

            return res;
        }

        public bool CanReplaceModerator(Member oldMember, Member newMember, GSubForum subForum)
        {
            GThread last = subForum.GetLastMessage(oldMember);
            bool res = this.CanAddModerator(newMember, subForum) &&
                   subForum.ContainsModerator(oldMember) &&
                   ((last == null) || (last.PublishDate < DateTime.Now.AddHours(-1)));

            return res;
        }

        public bool CanReplaceAdmin(Member currentAdmin, Member newAdmin)
        {
            return true;
        }

        public bool IsMessageCompatibleToSubForum(GSubForum gSubForum, GThread newMessage)
        {
            return true;
        }

        public bool CanBanMember(Member requesting, Member member)
        {
            return (requesting.UserType == UserType.Admin || requesting.UserType == UserType.Moderator)
            && MemberDA.getMemberDA().isLawBreaker(member.Username);
        }

        public bool IsNotification(Member member,GThread thread)
        {
            if (member == null)
                return false;
            GSubForum sf = thread.GetSubForum();
            List<Member> postedMembers = SubForumDA.GetSubForumDA().getMembersPosted(sf);
            bool res = postedMembers.Contains(member);

            if (thread is GPost)
                res |= ThreadDA.GetThreadDA().getTopicRoot((GPost)thread).publishedBy.Equals(member);
            
            res &= !thread.publishedBy.Equals(member);

            return res ;
        }

        public bool CanSubscribe(string username)
        {
            if (GForum.GetForum().IsMember(username))
            {
                return GForum.GetForum().GetMember(username).IsActive;
            }

            return true;
        }

        #endregion
    }
}
